<?php

/**
 * 自动生成 MerDictionaryController
 * 
 * Apps/Mp/Controller/MerDictionaryController.class.php
 * Zix @ 2015-07-02
 */

namespace Mp\Controller;

use Common\Service\MerDictionaryService ;
use Common\Service\MerchantService ;

class MerDictionaryController extends MpController {

  public function index( $pid = 0 ) {
    $action = I('get.action', 'read');

    $data = $this->_init_data();
        
    $data['module_name'] = '字典管理';
    $data['module_url'] = U( $this->base_path . 'index') . '/';
    
    $MerDictionary = MerDictionaryService::instance();
    $data['config'] = array(
      'action' => $action,
      'action_label' => $this->action_label[$action],
      'menu_id' => $this->menu_id ,
      'mer_id' => $this->mer_id ,
      'pid' => $pid ,
      'mer_id' => $data['mer_id'] ,
      'module_url' => U( $this->base_path . 'index' ),
      'read_url'   => U( $this->base_path . 'read'  ),
      'create_url' => U( $this->base_path . 'create'),
      'update_url' => U( $this->base_path . 'update'),
      'delete_url' => U( $this->base_path . 'delete'),
      'page' => 1,
      'page_size' => C('PAGE_SIZE'),
      'row' => $MerDictionary->get_default_row() 
    );

    $data['pdata'] = array();
    if ( !empty( $pid ) ) {
      $data['pdata'] = $MerDictionary->get_by_id($pid);
    }
    
    $data['param'] = array(
      'is_del' => $MerDictionary->is_del
    );
    $this->display_with_layout( $data , $this->controller . ':index' );
  }

  function read() {
    $post = I('post.');

    $config = array(
      'page' => $post['page'],
      'page_size' => $post['page_size'],
      'pid' => $post['pid'] ,
      'mer_id' => $this->mer_id ,
      'sort' => empty($post['sort']) ? 'sort' : $post['sort'],
      'order' => empty($post['order']) ? 'ASC' : $post['order'],
    );

    $MerDictionary = MerDictionaryService::instance();
    $data['rows'] = $MerDictionary->get_by_cond($config);

    $config['count'] = TRUE;
    $data['total'] = $MerDictionary->get_by_cond($config);

    $this->ajaxReturn($data);
  }

  function create() {
    $post = I('post.');
    if (empty($post)) {
      $this->ajaxReturn(ajax_arr('数据错误,请稍后再试'));
    }
    
    if( empty( $post['pid'] ) ) {
      unset( $post['pid']) ;
    }
    
    $post['mer_id'] = $this->mer_id;
    $MerDictionary = MerDictionaryService::instance();
    $ret = $MerDictionary->create($post);
    $this->ajaxReturn($ret);
  }

  function update($id = 0) {
    $post = I('post.');
    if (empty($post)) {
      $this->ajaxReturn(ajax_arr('数据错误,请稍后再试'));
    }

    if( empty( $post['pid'] ) ) {
      unset( $post['pid']) ;
    }
    $post['update_date'] = date('Y-m-d H:i:s');
    $MerDictionary = MerDictionaryService::instance();
    $ret = $MerDictionary->update($id, $post);
    $this->ajaxReturn($ret);
  }

  function delete() {
    $ids = I('post.ids');
    $MerDictionary = MerDictionaryService::instance();
    $ret = $MerDictionary->delete($ids); 
    $this->ajaxReturn($ret);
  }
}
